package com.work.keep.activity;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.work.keep.adapter.MyFragmentPagerAdapter;
import com.work.keep.R;
import com.work.keep.adapter.OnColorChangable;
import com.work.keep.base.BaseActivity;
import com.work.keep.fragment.DiscoverFragment;
import com.work.keep.fragment.FollowsFragment;
import com.work.keep.fragment.NewestFragment;

import java.util.ArrayList;
import java.util.List;

public class DynamicActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener, OnColorChangable {
    private Toolbar toolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private NavigationView navigationView;
    private FloatingActionButton fab;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private List<Fragment> fragments;
    private DiscoverFragment discoverFragment;
    private FollowsFragment followsFragment;


    @Override
    protected View getLayoutView() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_recommend;
    }

    @Override
    protected void initView() {
        toolbar = (Toolbar) findViewById(R.id.myToolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        navigationView = (NavigationView) findViewById(R.id.myNV);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    }

    @Override
    protected void initData() {
        toolbar.setTitle("Keep");
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_0:
                        Toast.makeText(DynamicActivity.this, "fuck", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        navigationView.setNavigationItemSelectedListener(this);
        fab.setOnClickListener(this);
        initViewPager();
    }

    private void initViewPager() {
        fragments = new ArrayList<>();
        discoverFragment = new DiscoverFragment();
        followsFragment = new FollowsFragment();
        fragments.add(discoverFragment);
        fragments.add(followsFragment);
        fragments.add(new NewestFragment());
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments, this);
        viewPager.setAdapter(myFragmentPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < fragments.size(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(myFragmentPagerAdapter.getTabView(i));
        }

        if (tabLayout.getTabAt(0).isSelected()) {
            viewPager.setCurrentItem(0);
            discoverFragment.setOnColorChangable(DynamicActivity.this);
        }



        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                viewPager.setCurrentItem(position);

                if (position == 1) {
                    followsFragment.setOnColorChangable(DynamicActivity.this);
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                Log.d("GG", "un");
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                Log.d("GG", "re");
            }
        });
    }

//    private void extract(Bitmap bitmap) {
//        Palette.Builder palette = Palette.from(bitmap);
//        palette.generate(new Palette.PaletteAsyncListener() {
//            @Override
//            public void onGenerated(Palette palette) {
//                Palette.Swatch vibrant = palette.getVibrantSwatch();    // 有活力的颜色
//                Palette.Swatch darkVibrant = palette.getDarkVibrantSwatch();    // 有活力的暗色
//                Palette.Swatch lightVibrant = palette.getLightVibrantSwatch();    // 有活力的亮色
//                Palette.Swatch muted = palette.getMutedSwatch();    // 柔和的颜色
//                Palette.Swatch darkMuted = palette.getDarkMutedSwatch();    // 柔和的暗色
//                Palette.Swatch lightMuted = palette.getLightMutedSwatch();    // 柔和的亮色
//                if (vibrant != null) {
//                }
//
//                if (darkVibrant != null) {
//                }
//
//                if (lightVibrant != null) {
//                }
//
//                if (muted != null) {
//                }
//
//                if (darkMuted != null) {
//                }
//
//                if (lightMuted != null) {
//                }
//            }
//        });


//    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(DynamicActivity.this, "Floating", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void changeColor(int color) {
        tabLayout.setBackgroundColor(color);
    }
}
